package com.lechuang.cunpinke.network;

import com.lechuang.cunpinke.activity.BaseActivity;
import com.lechuang.cunpinke.bean.ResultBean;
import com.lechuang.cunpinke.fragment.BaseFragment;
import com.lechuang.cunpinke.fragment.BaseFragmentForV4;
import com.lechuang.cunpinke.manager.ToastManager;
import com.lechuang.cunpinke.utils.LogUtils;
import com.tencent.bugly.crashreport.CrashReport;

import rx.Observer;

/**
 * 作者：尹松涛
 * 邮箱：yinsongtaoshmily@outlook.com
 * 日期：2017/9/29
 * 时间：09:19
 */

public abstract class NetResultHandler<T> implements Observer<ResultBean<T>> {
    private BaseActivity activity;
    private BaseFragment fragment;
    private BaseFragmentForV4 fragmentForV4;
    private boolean isShowToast;
    private String tag;

    public NetResultHandler(BaseActivity activity, boolean isShowToast) {
        this.activity = activity;
        this.isShowToast = isShowToast;
        tag = activity.getClass().getSimpleName();
    }

    public NetResultHandler(BaseFragment fragment, boolean isShowToast) {
        this.fragment = fragment;
        this.isShowToast = isShowToast;
        tag = fragment.getClass().getSimpleName();
    }

    public NetResultHandler(BaseFragmentForV4 fragmentForV4, boolean isShowToast) {
        this.fragmentForV4 = fragmentForV4;
        this.isShowToast = isShowToast;
        tag = fragmentForV4.getClass().getSimpleName();
    }

    @Override
    public void onCompleted() {
        if (activity != null) activity.hideWaitDialog();
        if (fragment != null) fragment.hideWaitDialog();
        if (fragmentForV4 != null) fragmentForV4.hideWaitDialog();
    }

    @Override
    public void onError(Throwable e) {
        if (activity != null)
            ExceptionHandler.handlerException(activity, e, isShowToast);
        if (fragment != null)
            ExceptionHandler.handlerException(fragment, e, isShowToast);
        if (fragmentForV4 != null)
            ExceptionHandler.handlerException(fragmentForV4, e, isShowToast);
    }

    @Override
    public void onNext(ResultBean<T> result) {
        if (result.errorCode == 200) {
            success(result.data);
        } else if (result.errorCode == 401 || result.errorCode == 303) {
            handlerLogin(result.errorCode);
        } else if (result.errorCode == 300) {
            LogUtils.e(tag, "网络传输参数有误");
            CrashReport.postCatchedException(new Exception("网络传输参数有误"));
            ToastManager.getInstance().showShortToast("网络不给力，建议重新打开APP重试");
        } else {
            LogUtils.e(tag, "服务器返回未知错误");
            CrashReport.postCatchedException(new Exception("服务器返回未知错误"));
            ToastManager.getInstance().showShortToast("出现错误，请稍后重试");
        }
    }

    /**
     * 处理 用户需要登录及未授权的问题
     *
     * @param errorCode 服务器返回的错误码
     */
    protected void handlerLogin(int errorCode) {

    }

    public abstract void success(T result);

    static class ExceptionHandler {
        public static void handlerException(BaseActivity activity, Throwable throwable, boolean isShowToast) {
            activity.hideWaitDialog();
            CrashReport.postCatchedException(throwable);
            if (isShowToast) {
                ToastManager.getInstance().showShortToast("网络不给力请检查网络");
            }
        }

        public static void handlerException(BaseFragment fragment, Throwable throwable, boolean isShowToast) {
            fragment.hideWaitDialog();
            CrashReport.postCatchedException(throwable);
            if (isShowToast) {
                ToastManager.getInstance().showShortToast("网络不给力请检查网络");
            }
        }

        public static void handlerException(BaseFragmentForV4 fragmentForV4, Throwable throwable, boolean isShowToast) {
            fragmentForV4.hideWaitDialog();
            CrashReport.postCatchedException(throwable);
            if (isShowToast) {
                ToastManager.getInstance().showShortToast("网络不给力请检查网络");
            }
        }
    }

}
